package com.study.builder;

/**
 * 建造者模式 测试
 * @author ：hao_lai ∶）
 * @date ：Created in 2020/12/6 23:35
 */
public class UserInfo {
    private String name;
    private String address;
    private Integer age;
    private String wechat;

    public UserInfo(Builders builders) {
        this.name = builders.name;
        this.address = builders.address;
        this.age = builders.age;
        this.wechat = builders.wechat;
    }

    /**
     * 调用builders直接创建builder对象 不需要再new
     * @return 建造者对象
     */
    public static Builders builders() {
        return new Builders();
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", age=" + age +
                ", wechat='" + wechat + '\'' +
                '}';
    }

    /**
     * 由此进行builder链式编程
     * 由于每次返回的是builders对象则可以进行下一个参数传输
     */
    public static class Builders {
        private String name;
        private String address;
        private Integer age;
        private String wechat;

        public Builders name(String name) {
            this.name = name;
            return this;
        }

        public Builders address(String address) {
            this.address = address;
            return this;
        }

        public Builders age(Integer age) {
            this.age = age;
            return this;
        }

        public Builders wechat(String wechat) {
            this.wechat = wechat;
            return this;
        }

        /**
         * 最后返回对象
         * @return 对象
         */
        public UserInfo builder() {
            return new UserInfo(this);
        }
    }
}
